package yx.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yx.dao.BaseDaoInt;
import yx.model.BsTree;
import yx.vo.AddrbookVO;
import yx.vo.TreeVO;

@Service("treeService")
public class TreeServiceImpl implements TreeServiceInt {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TreeServiceImpl.class);

	private BaseDaoInt<BsTree> treeDao;

	public BaseDaoInt<BsTree> getTreeDao() {
		return treeDao;
	}

	@Autowired
	public void setTreeDao(BaseDaoInt<BsTree> treeDao) {
		this.treeDao = treeDao;
	}

	@Override
	public TreeVO save(TreeVO tree) {
		BsTree t = new BsTree();
		BeanUtils.copyProperties(tree, t);

		// 设置上级文档类型
		BsTree p = new BsTree();
		p.setId(tree.getPid());
		t.setBsTree(p);

		treeDao.save(t);
		BeanUtils.copyProperties(t, tree);
		return tree;
	}

	@Override
	public void removeById(String id) {
		treeDao.executeHql("delete BsTree t where t.id = '" + id + "'");
	}

	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete BsTree t where t.id in (";
		for (int i = 0; i < nids.length; i++) {
			if (i > 0) {
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		treeDao.executeHql(hql);
	}

	@Override
	public List<TreeVO> getTreeNode(String id) {
		List<TreeVO> nl = new ArrayList<TreeVO>();
		String hql = null;
		Map<String, Object> params = new HashMap<String, Object>();
		if (id == null || "".equals(id)) {
			// 查询所有根节点
			hql = "from BsTree t where t.tree is null";
		} else {
			// 查询当前id下的所有子节点
			hql = "from BsTree t where t.tree.id = :id";
			params.put("id", id);
		}
		List<BsTree> l = this.treeDao.find(hql, params);
		if (l != null && l.size() > 0) {
			for (BsTree t : l) {
				TreeVO m = new TreeVO();
				BeanUtils.copyProperties(t, m);
				Set<BsTree> set = t.getBsTrees();
				if (set != null && !set.isEmpty()) {
					m.setState("closed"); // 节点以文件夹的形式体现
				} else {
					m.setState("open"); // 节点以文件的形式体现
				}
				nl.add(m);
			}
		}
		return nl;
	}

	@Override
	public List<TreeVO> getAllTreeNode(TreeVO tree) {
		List<TreeVO> nl = new ArrayList<TreeVO>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = "from BsTree t where 1 = 1";
		hql = addWhere(tree, hql, params);
		List<BsTree> l = this.treeDao.find(hql,params);
		if (l != null && l.size() > 0) {
			for (BsTree t : l) {
				TreeVO tt = new TreeVO();
				BeanUtils.copyProperties(t, tt);
				Map<String, Object> attributes = new HashMap<String, Object>();
				attributes.put("url", t.getUrl());
				tt.setAttributes(attributes);
				BsTree p = t.getBsTree();
				if (p != null) {
					tt.setPid(t.getBsTree().getId());
				}
				nl.add(tt);
			}
		}
		return nl;
	}

	private String addWhere(TreeVO tree, String hql, Map<String, Object> params) {
		if (tree.getType() != null) {
			hql += " and t.type like :type";
			params.put("type", tree.getType());
		}
		return hql;
	}

}
